from flask import Flask
from flask import request
import json
from block import Block
from game import *
app = Flask(__name__)

@app.route('/nextMove')
def nextMove():
    value = request.args.get('value')
    print(value)
    value = json.loads(value)
    b = Block(value)
    b.print()
    if(b.isDead()):
        print('lose')
        return 'stop'
    if(b.isWin(11)):
        print('win?')
        return 'stop'
    g = Game(b)
    # g._block.print()


    move = g.getBest()
    if move['move'] == None:
        return 'up';
    if(move['move'] < 0):
        return 'stop'
    move['move'] = tranDirection(move['move']);
    print(move)
    
    return move['move']

if __name__ == '__main__':
    app.run(host='192.168.1.141',port=5000)

#
# @app.route('/login', methods=['POST', 'GET'])
# def login():
#     error = None
#     if request.method == 'POST':
#         if valid_login(request.form['username'],
#                        request.form['password']):
#             return log_the_user_in(request.form['username'])
#         else:
#             error = 'Invalid username/password'
#     # the code below is executed if the request method
#     # was GET or the credentials were invalid
#     return render_template('login.html', error=error)